package com.example.qihuibao

import androidx.multidex.MultiDexApplication
import androidx.room.Room
import com.example.qihuibao.dao.MyDatabase
import com.example.qihuibao.utils.KTCommonUtils
import com.github.anrwatchdog.ANRWatchDog
import com.tencent.bugly.crashreport.CrashReport
import com.tencent.mmkv.MMKV

/**
 *@author: cww
 *@desc:
 *@date: 4/18/23 7:33 PM
 */
class XApplication : MultiDexApplication() {
    companion object {
        lateinit var application:XApplication
    }

    override fun onCreate() {
        super.onCreate()
        application = this
        initBugly()
        initMMKV()
        initDB()
        initWatchDog()
    }

    private fun initBugly() {
        CrashReport.initCrashReport(this, "60b0ec3a69", false)
//        CrashReport.testJavaCrash();//test
    }

    private fun initMMKV() {
        MMKV.initialize(this);
//        Log.e("cww", "v1=${MMKVUtils.saveValue("key4", 1.1f)}")//test
//        Log.e("cww", "v1=${MMKVUtils.getValue("key4", 1.0f)}")
    }

    private fun initDB() {
        MyDatabase.db = Room.databaseBuilder(application, MyDatabase::class.java, "qhb.db")
//            .addMigrations(MyDatabase.MIGRATION_1_2)
            .build()
    }

    //添加看门狗，ANR时(5秒)重启应用
    private fun initWatchDog() {
        ANRWatchDog().setANRListener { KTCommonUtils.restartApp(application) }.start()
    }

}